//
//  ChooseCategoryAlertView.m
//  GC_OC
//
//  Created by tuibian  on 2022/5/30.
//

#import "ChooseCategoryAlertView.h"
#import "OneLevelLinkerCell.h"
@interface ChooseCategoryAlertView ()
@property(nonatomic, strong)UIView *bgView;
@property(nonatomic, strong)UIButton *importButton;
@property(nonatomic, strong)UILabel *categoryLabel;

@end

@implementation ChooseCategoryAlertView
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.bgView = [UIView new];
        self.bgView.backgroundColor= XHUIColorFromRGBA(0x000000, 0.24);
        [self addSubview:self.bgView];
        
        UIView *coverView = [UIView new];
        coverView.backgroundColor = [UIColor whiteColor];
        coverView.layer.masksToBounds = YES;
        coverView.layer.cornerRadius = 8;
        [self.bgView addSubview:coverView];
        
        UILabel *title = [UILabel new];
        title.text = @"选择分类";
        title.textAlignment = NSTextAlignmentCenter;
        title.textColor = [UIColor blackColor];
        title.font = [UIFont systemFontOfSize:18 weight:UIFontWeightMedium];
        [coverView addSubview:title];
        
        UIButton *backButton = [UIButton new];
        [backButton setImage:[UIImage imageNamed:@"icon清空32default备2"] forState:UIControlStateNormal];
        [backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
        [coverView addSubview:backButton];
        
        UIView *view = [UIView new];
        view.backgroundColor = XHUIColorFromRGBA(0xEAE0E4, 0.3);
        [coverView addSubview:view];
        
        self.categoryLabel = [UILabel new];
        self.categoryLabel.textColor = [UIColor blackColor];
        self.categoryLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
        [view addSubview:self.categoryLabel];
        
        UIButton *importButton = [UIButton new];
        importButton.layer.masksToBounds = YES;
        self.importButton = importButton;
        importButton.layer.cornerRadius = 22.5;
        [importButton setTitle:@"确认" forState:UIControlStateNormal];
        [importButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        importButton.backgroundColor = XHUIColorFromRGBA(0x0091FF, 0.16);
        importButton.titleLabel.font = [UIFont systemFontOfSize:16];
        [importButton addTarget:self action:@selector(importAction) forControlEvents:UIControlEventTouchUpInside];
        
        
        [coverView addSubview:importButton];
        
        //tableView
        self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        self.tableView.backgroundColor = [UIColor whiteColor];

        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.tableView.dataSource = self;
        self.tableView.delegate = self;
        [self.tableView registerClass:[OneLevelLinkerCell class] forCellReuseIdentifier:@"OneLevelLinkerCell"];
        [coverView addSubview:self.tableView];
        
        
        [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
        
        [coverView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.bgView).offset(15);
            make.right.equalTo(self.bgView).offset(-15);
            make.top.equalTo(self.bgView).offset(122);
            make.bottom.equalTo(self.bgView.mas_bottom).offset(-50);
        }];
        [title mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(coverView).offset(47);
            make.right.equalTo(coverView).offset(-38);
            make.top.equalTo(coverView).offset(15);
            make.height.mas_equalTo(25);
        }];
        [backButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(coverView).offset(-11);
            make.centerY.equalTo(title).offset(0);
            make.height.width.mas_equalTo(32);
        }];
        
        [importButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(45);
            make.width.mas_equalTo(185);
            make.centerX.equalTo(coverView);
            make.bottom.equalTo(coverView).offset(-20);
        }];
        
        [self.categoryLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(view).offset(30);
            make.height.mas_equalTo(44);
            make.right.top.equalTo(view);
        }];
        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(coverView);
            make.height.mas_equalTo(44);
            make.top.equalTo(title.mas_bottom).mas_equalTo(10);
        }];
        
        [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(importButton.mas_top).mas_equalTo(-30);
            make.left.right.equalTo(coverView);
            make.top.equalTo(view.mas_bottom).mas_equalTo(30);
        }];
        
        self.alpha = 0.0;
        
    }
    return self;
}

- (void)showIn:(UIView *)view {
    self.frame = view.bounds;
//    [view addSubview:self];
    [self refreshCategoryTitle];
    [self.tableView reloadData];
    [UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self.alpha = 1.0;
    } completion:nil];
}

- (void)back {
    [UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self.alpha = 0.0;
    } completion:^(BOOL finished) {
//        [self removeFromSuperview];
    }];
}

#pragma mark - tableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataSource.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    LinkerModel *obj = self.dataSource[section];
    if ([obj.isFold isEqual:@0]) {
        return 0;
    }
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    OneLevelLinkerCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OneLevelLinkerCell"];
//    if (indexPath.row == 0) {
//        cell.backgroundColor = [UIColor greenColor];
//    }
     LinkerModel *info = self.dataSource[indexPath.section];
    cell.indexPath = indexPath;
    cell.fdataSource = self.dataSource;
    cell.isEditor = NO;
    cell.chooseType = YES;
    [cell configCell:info.list];

    __weak typeof(self) ws = self;

    cell.tapTwoLevelBlock = ^(NSInteger findex, NSInteger sindex) {
        for (LinkerModel *obj in ws.dataSource) {
            obj.isSelect = @0;
            
            NSInteger i = [ws.dataSource indexOfObject:obj];
            if (i != findex) {
                obj.isSelect = @0;
            } else {
                obj.isSelect = @1;
            }

            for (LinkerModel *sobj in obj.list) {
                sobj.isSelect = @0;

                for (LinkerModel *tobj in sobj.list) {
//                    tobj.isSelect = @0;
                }
            }

        }

        LinkerModel *obj = ws.dataSource[findex];
        LinkerModel *sobj = obj.list[sindex];
        
        sobj.isSelect = [sobj.isSelect isEqual:@0] ? @1: @0;
        if ([sobj.isSelect isEqual:@1]) {
            obj.isSelect = @1;
        }
        [ws.tableView reloadData];
        
        ws.importButton.backgroundColor = XHUIColorFromRGBA(0x0091FF, 1);
        [ws refreshCategoryTitle];
    };

    cell.tapThirdLevelBlock = ^(NSInteger findex, NSInteger sindex, NSInteger tindex) {
        for (LinkerModel *obj in ws.dataSource) {
            for (LinkerModel *sobj in obj.list) {
                for (LinkerModel *tobj in sobj.list) {
                    tobj.isSelect = @0;
                }
            }
        }

        for (NSInteger i = 0; i < ws.dataSource.count; i++) {
            if (i != findex) {
                ws.dataSource[i].isSelect = @0;

                LinkerModel *fobj = ws.dataSource[i];
                for (NSInteger j = 0; j < fobj.list.count; j++) {
                    LinkerModel *sobj = fobj.list[j];
                    sobj.isSelect = @0;
                }
            } else {
                LinkerModel *fobj = ws.dataSource[i];
                for (NSInteger j = 0; j < fobj.list.count; j++) {
                    if (j != sindex) {
                        LinkerModel *sobj = fobj.list[j];
                        sobj.isSelect = @0;
                    }
                }
            }
        }


        LinkerModel *obj = ws.dataSource[findex];
        LinkerModel *sobj = obj.list[sindex];
        LinkerModel *tobj = sobj.list[tindex];
        obj.isSelect = @1;
        sobj.isSelect = @1;
        tobj.isSelect = @1;
        [ws.tableView reloadData];

        ws.fModel = obj;
        ws.sModel = sobj;
        ws.tModel = tobj;
        
        [ws refreshCategoryTitle];
    };
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 38;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    LinkerModel *info = self.dataSource[section];
    NSString *name = info.name;

    UIView *hView = [UIView new];
    hView.tag = 2000 + section;
    hView.backgroundColor = [UIColor clearColor];

    UITapGestureRecognizer *hTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapHeaderView:)];
    [hView addGestureRecognizer:hTap];


    UIView *vline = [UIView new];
    vline.backgroundColor = XHUIColorFromRGB(0x979797);
    [hView addSubview:vline];

    UIView *hline = [UIView new];
    hline.backgroundColor = XHUIColorFromRGB(0x979797);
    [hView addSubview:hline];

    UIView *aidvline = [UIView new];
    aidvline.backgroundColor = XHUIColorFromRGB(0x979797);
    [hView addSubview:aidvline];

    UILabel *hTitle = [UILabel new];
    if ([info.isSelect isEqual:@1]) {
        hTitle.textColor = XHUIColorFromRGB(0x0091FF);
    } else {
        hTitle.textColor = [UIColor blackColor];
    }
    hTitle.text = name;
    hTitle.font = [UIFont systemFontOfSize:20 weight:UIFontWeightMedium];
    [hView addSubview:hTitle];

    //添加
    UIButton *addButton = [UIButton new];
    [addButton setImage:[UIImage imageNamed:@"icon_返回_32_line备份_3"] forState:UIControlStateNormal];
    [addButton addTarget:self action:@selector(addAction:) forControlEvents:UIControlEventTouchUpInside];
    [hView addSubview:addButton];
    //删除
    UIButton *deleteButton = [UIButton new];
    [deleteButton setImage:[UIImage imageNamed:@"icon_返回_32_line备份_2"] forState:UIControlStateNormal];
//    [deleteButton addTarget:self action:@selector(deleteAction:) forControlEvents:UIControlEventTouchUpInside];
    [hView addSubview:deleteButton];
    //编辑
    UIButton *editButton = [UIButton new];
    [editButton setImage:[UIImage imageNamed:@"icon_重命名_24_line备份"] forState:UIControlStateNormal];
//    [editButton addTarget:self action:@selector(editorAction:) forControlEvents:UIControlEventTouchUpInside];
    [hView addSubview:editButton];

    [hline mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(hView).offset(35);
        make.centerY.equalTo(hView);
        make.height.mas_equalTo(1);
        make.width.mas_equalTo(7);
    }];
    [vline mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(hline);
        make.top.equalTo(hline.mas_bottom).offset(0);
        make.bottom.equalTo(hView);
        make.width.mas_equalTo(1);
    }];

    [aidvline mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(hline);
        make.top.equalTo(hView);
        make.height.mas_equalTo(19);
        make.width.mas_equalTo(1);
    }];

    [hTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(hView).offset(43);
        make.centerY.equalTo(hView);
        make.height.mas_equalTo(18);
//        make.right.equalTo(hView).offset(-20);
    }];

    [addButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(hTitle.mas_right).offset(12);
        make.centerY.equalTo(hTitle);
        make.height.width.mas_equalTo(24);
    }];

    [deleteButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(addButton.mas_right).offset(5);
        make.centerY.equalTo(hTitle);
        make.height.width.mas_equalTo(24);

    }];

    [editButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(deleteButton.mas_right).offset(5);
        make.centerY.equalTo(hTitle);
        make.height.width.mas_equalTo(24);
    }];

    aidvline.hidden = NO;
    vline.hidden = NO;
    if (section == 0) {
        aidvline.hidden = YES;
    }
    if (section == self.dataSource.count - 1) {
        vline.hidden = YES;
    }

    addButton.hidden = YES;
    deleteButton.hidden = YES;
    editButton.hidden = YES;

    hline.hidden = NO;
    if (self.dataSource.count == 1) {
        hline.hidden = YES;
    }

    return hView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    //计算行高
    LinkerModel *userInfo = self.dataSource[indexPath.section];
    NSMutableArray *list = userInfo.list;
    CGFloat towLevelHeight = list.count * 38;

    for (LinkerModel *info in list) {
        NSNumber *isFold = info.isFold;
        NSArray *tempList = info.list;
        if ([isFold isEqual:@1]) {
            towLevelHeight += 40 * tempList.count;
        }
    }

    return towLevelHeight;
}

- (void)tapHeaderView:(UIGestureRecognizer *) sender {
    UIView *hView = sender.view;
    NSInteger index = hView.tag - 2000;

    for (LinkerModel *obj in self.dataSource) {
        if ([self.dataSource indexOfObject:obj] != index) {
            obj.isSelect = @0;
            for (LinkerModel *sObj in obj.list) {
                sObj.isSelect = @0;
            }
        }
    }
    LinkerModel *obj = self.dataSource[index];
    if ([obj.isFold isEqual:@1]) {
        obj.isFold = @0;
    } else {
        obj.isFold = @1;
    }
    if ([obj.isSelect isEqual:@1]) {
        obj.isSelect = @0;
    } else {
        obj.isSelect = @1;
    }
    [self.tableView reloadData];
    
    self.importButton.backgroundColor = XHUIColorFromRGBA(0x0091FF, 0.16);
    for (LinkerModel *obj in self.dataSource) {
        for (LinkerModel *sObj in obj.list) {
            if ([sObj.isSelect isEqual:@1]) {
                self.importButton.backgroundColor = XHUIColorFromRGB(0x0091FF);
            }
        }

    }
    
    [self refreshCategoryTitle];
}

- (void)importAction {
    LinkerModel *fModel;
    LinkerModel *sModel;
    for (LinkerModel *obj in self.dataSource) {
        for (LinkerModel *model in obj.list) {
            if ([model.isSelect isEqual:@1]) {
                fModel = obj;
                sModel = model;
            }
        }
    }
    if (fModel && sModel) {
        if (self.confirmBlock != nil) {
            self.confirmBlock();
        }
        [self back];

    } else {
//        [[AppDelegate shared].window makeToast:@"请选择分类"];
    }
    
    
}

- (void)refreshCategoryTitle {
    NSString *text = @"";
    for (LinkerModel *fobj in self.dataSource) {
        if ([fobj.isSelect isEqual:@1]) {
            text = fobj.name;
        }
        for (LinkerModel *sobj in fobj.list) {
            if ([sobj.isSelect isEqual:@1]) {
                
                text = [NSString stringWithFormat:@"%@-%@",text, sobj.name];
            }
        }
    }
    self.categoryLabel.text = text;
    
    self.importButton.backgroundColor = XHUIColorFromRGBA(0x0091FF, 0.16);
    for (LinkerModel *obj in self.dataSource) {
        for (LinkerModel *sObj in obj.list) {
            if ([sObj.isSelect isEqual:@1]) {
                self.importButton.backgroundColor = XHUIColorFromRGB(0x0091FF);
            }
        }

    }
}


@end
